package c2.cross.business.ladder;

import c2.advanced.business.base.KvData;
import c2.cross.business.CrossBody;
import c2.cross.business.base.CrossModule;
import c2.cross.business.base.CrossSubModule;
import c2.cross.business.ladder.common.LadderOppoDetail;
import c2.cross.business.ladder.common.LadderStatus;
import c2.cross.business.ladder.rank.LadderRankModule;
import c2.cross.business.ladder.record.LadderRecordModule;
import cate.common.game.mail.MailCmd;
import cate.common.table.d.GD;
import cate.common.table.d.GDMail;
import cate.common.table.d.GDObj;
import cate.common.table.d.GDOperation;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import com.google.common.collect.Lists;
import cp.solution.business.game.component.NoticeParam;
import lombok.extern.slf4j.Slf4j;

import java.util.Optional;

/**
 * 跨服天梯赛总模块
 */
@Slf4j
public class LadderModule extends CrossModule<LadderStatus> {

    public LadderRankModule rank = new LadderRankModule();
    public LadderRecordModule records = new LadderRecordModule();

    public LadderModule(CrossBody context) {
        super(context);
    }

    @Override
    protected CrossSubModule<?>[] getSubModules() {
        return new CrossSubModule[]{rank, records};
    }

    @Override
    protected String getStatusKey() {
        return KvData.KEY_LADDER;
    }

    @Override
    protected Class<LadderStatus> getStatusClass() {
        return LadderStatus.class;
    }

    @Override
    public String getName() {
        return "天梯赛";
    }

    @Override
    protected void onShutdownAfterSubModules() {
        log.info("天梯赛onShutdownAfterSubModules");
    }

    public void onSunday2300() {
        //发送奖励邮件
        for (LadderOppoDetail ladderOppoDetail : rank.getList()) {
            if (!ladderOppoDetail.robot) {
                String rewards = context.table.ladder.seasonRwd.getReward(ladderOppoDetail.rank);
                int starMax = Optional.ofNullable(help().getSnapshot(ladderOppoDetail.id))
                        .map(snapshot -> snapshot.shareStarMax)
                        .orElse(0);
                int extra = table().pvpRankPointExtra.base.getExtraParam(starMax);
                MailCmd mail;
                if (extra > 0) {
                    MixRes res = new MixRes(rewards);
                    long count = res.count(GDObj.Type.NUM, 14);
                    long addNum = count * extra / GD.W_10000;
                    MixResItem item = new MixResItem(GDObj.Type.NUM, 14, addNum);
                    res.addItem(item);
                    mail = new MailCmd()
                            .withContent(GDMail.Tid.LADDER_RACE_SEASON_RWD_EX,
                                    Lists.newArrayList(
                                            new NoticeParam(ladderOppoDetail.rank),
                                            new NoticeParam(starMax),
                                            new NoticeParam((extra / 100.0) + "%"),
                                            new NoticeParam(NoticeParam.NoticeType.MIX_RES, new MixRes().addItem(item))))
                            .withReward(res.toItemsStr());
                } else {
                    mail = new MailCmd()
                            .withContent(GDMail.Tid.LADDER_RACE_SEASON_RWD,
                                    Lists.newArrayList(
                                            new NoticeParam(ladderOppoDetail.rank)
                                    ))
                            .withReward(rewards);
                }
                mail.addRole(ladderOppoDetail.id)
                        .withSrvId(ladderOppoDetail.srvId)
                        .withOpr(GDOperation.LADDER_SEASON_RWD);
                //重置用户天梯数据
                log.info("天梯赛排行榜结算后清空,重置用户自身天梯数据");
                rank.reset(ladderOppoDetail.srvId, ladderOppoDetail.id);

                try {
                    context.common.sendMail(mail);
                    log.info("天梯赛排行榜结算发送奖励邮件给用户id={},奖励={}", ladderOppoDetail.id, rewards);
                } catch (Exception e) {
                    log.error("天梯赛排行榜结算发送奖励邮件给用户id={}失败", ladderOppoDetail.id, e);
                }
            }
        }
        // 排行榜清空
        // 添加清空排行榜的逻辑, 并且最好加上比较详细的log
        log.info("天梯赛排行榜结算后清空,重新生成机器人");
        rank.clearList();
        rank.resetListWithRobots();
        log.info("天梯赛排行榜结算后清空,机器人全量入库");
        rank.saveRankList();
        log.info("天梯赛大神风采结算后清空");
        records.clearRecords();
        log.info("天梯赛大神风采数据库结算后清空");
        records.removeRecords();
        // 因为根据以往项目经验，常常会有玩家反馈没有收到奖励邮件，此时，我们就需要通过日志去排查问题。
    }
}
